package dao;

import mappers.CommentMapper;
import model.Comment;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import java.util.List;

public class CommentDAO implements CommentMapper {
    @Override
    public void insertComment(Comment comment) {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
        try {
            CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
            commentMapper.insertComment(comment);
            sqlSession.commit();
        }
        finally {
            sqlSession.close();
        }
    }

    @Override
    public List<Comment> getCommentsByDocumentId(int id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
        try {
            CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
            return commentMapper.getCommentsByDocumentId(id);
        }
        finally {
            sqlSession.close();
        }
    }

    @Override
    public void deleteCommentById(int id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
        try {
            CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
            commentMapper.deleteCommentById(id);
            sqlSession.commit();
        }
        finally {
            sqlSession.close();
        }
    }
}
